package com.dz.scrm.mvp.ui.mine

import android.annotation.SuppressLint
import android.os.Bundle
import com.dz.scrm.R
import com.dz.scrm.app.config.WHAT_CHECK_VERSION
import com.dz.scrm.constant.*
import com.dz.scrm.mvp.model.entity.CheckVersionBean
import com.dz.scrm.mvp.presenter.DZPresent
import com.dz.scrm.mvp.ui.DZActivity
import com.dz.scrm.utils.*
import com.dz.scrm.view.UpdateVersionDialog
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.dz.scrm.utils.launchActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_about.*
import me.jessyan.art.mvp.IView
import me.jessyan.art.mvp.Message
import me.jessyan.art.utils.ArtUtils
import me.jessyan.art.utils.DeviceUtils

/**
 * 作者：fanda
 * 日期：2021/4/23 15:02
 * 描述：关于界面
 */
class AboutActivity : DZActivity<DZPresent>(), IView {

    private var checkVersionBean: CheckVersionBean? = null

    override fun initView(savedInstanceState: Bundle?) = R.layout.activity_about

    override fun initData(savedInstanceState: Bundle?) {
        checkVersionBean = getCheckVersionBean()
        initListeners()
    }

    @SuppressLint("SetTextI18n")
    private fun initListeners() {
        tv_version.text = "V${DeviceUtils.getVersionName(this)}"
        iv_back.setOnClickListener { finish() }
        layout_check_update.setOnClickListener {
            if (checkVersionBean == null) {
                mPresenter.checkVersion(Message.obtain(this))
            } else {
                if (DeviceUtils.getVersionCode(this) < checkVersionBean!!.versionNo) {
                    showCheckVersionDialog(checkVersionBean!!)
                } else {
                    showMessage(getString(R.string.latest_version))
                }

            }
        }

        //用户协议
        layout_user_agreement.setOnClickListener {
            launchActivity<HtmlActivity>(this) {
                putExtra(INTENT_URL, checkVersionBean?.privacyAgreement?:"")
                putExtra(INTENT_URL_TYPE, ACTIVITY_PRIVACY_AGREEMENT)
            }
        }

        //隐私政策
        layout_privacy_agreement.setOnClickListener {
            launchActivity<HtmlActivity>(this) {
                putExtra(INTENT_URL, checkVersionBean?.userAgreement?:"")
                putExtra(INTENT_URL_TYPE, ACTIVITY_USER_AGREEMENT)
            }
        }
    }

    override fun obtainPresenter() = DZPresent(ArtUtils.obtainAppComponentFromContext(this), RxPermissions(this))

    override fun handleMessage(message: Message) {
        when (message.what) {
            WHAT_CHECK_VERSION -> {
                val checkVersionBean = message.obj as CheckVersionBean?
                SharedPreferencesUtil.saveInfo(CACHE_CHECK_UPDATE, JsonHelper.parserObject2Json(checkVersionBean))
                checkVersionBean?.let {
                    showCheckVersionDialog(it)
                }
            }
        }
    }

    override fun showMessage(message: String) {
        ArtUtils.snackbarText(message)
    }

    override fun onDestroy() {
        super.onDestroy()
        DownLoadHelper.removeDownLoadCallBack()
    }
}